<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
</head>
<body>
	<script type="text/javascript">
		
		var lastX,lastY,lastZ;
		var isShake = false;
		var maxRange = 80;
		var minRange = 10;
		window.addEventListener('devicemotion',function(e){
			var motion = e.accelerationIncludingGravity;
			var x = Math.round(motion.x);
			var y = Math.round(motion.y);
			var z = Math.round(motion.z);
			if(typeof lastX == 'undefined' || typeof lastY == 'undefined' || typeof lastZ == 'undefined'){
				lastX = x;
				lastY = y;
				lastZ = z;
				return;
			}

			var dis = Math.abs(x - lastX) + Math.abs(y - lastY) + Math.abs(z - lastZ);

			if(dis > maxRange){
				isShake = true;
			}

			if(dis < minRange && isShake){
				isShake = false;
				alert('摇一摇');
			}

			lastX = x;
			lastY = y;
			lastZ = z;


		});
	</script>
</body>
</html>
